;
if (window.location.href.indexOf('laykj.cn') > -1) {
    axios.defaults.baseURL = 'http://www.laykj.cn/wherebuyAPI/'
} else if (window.location.href.indexOf('http://192.168.0.6/') > -1) {
    axios.defaults.baseURL = 'http://laykj.jiangda.info:8123/wherebuyAPI/'
} else {
    axios.defaults.baseURL = 'http://laykj.jiangda.info:8123/wherebuyAPI/'
}

const Axios = axios.create({
  timeout: 600000,
  withCredentials: true, // 是否允许带cookie这些
  headers: {
    "Content-Type": 'application/json;charset=UTF-8'
  }
});

// 添加请求拦截器
Axios.interceptors.request.use(
  config => {
  // 在发送请求之前做些什么
  let token = getCookie('accessToken')
  if (!token) {
    if (window.location.href.indexOf('register') > -1) {

    } else {
      window.location.href = '../register/login.html'
    }
  } else {
    config.headers['accessToken'] = token
  }
  return config;
},
  error => {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
Axios.interceptors.response.use(
    response => {
        console.log(response)
        if (response.data.errorCode === 1906) {
            clearCookie('accessToken')
            localStorage.removeItem('userInfo')
            localStorage.removeItem('cartBadge')
            localStorage.removeItem('categoryHistory')
            localStorage.removeItem('searchHistory')
            window.location.href = '../register/login.html'
            return false
        }
        // 对响应数据做点什么
        return Promise.resolve(response.data);
    },
    error => {
        console.log(error)
        // 对响应错误做点什么
        return Promise.reject(error);
    });

//设置cookie
function setCookie(name, value, exdays, domain) {
  let d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+d.toUTCString();
  let cookies = name + "=" + value + "; " + expires+"; path=/";//path=/是根路径

    if(domain){
        cookies = cookies + ';domain=' + domain
    }

    document.cookie = cookies
}

// 获取cookie
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        if (name === 'userId') {
            return Number(unescape(arr[2]));
        } else {
            return unescape(arr[2]);
        }
    } else {
        return null;
    }
}

// 删除cookie
function clearCookie(name) {
  setCookie(name, "", -1);
}


/*时间格式化函数
*用法new Date(1542274800000).Format('yy-MM-dd hh:mm:ss'); "2018-11-15 17:40:00"
*/
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

// 获取地址栏参数
function getUrlParam(paraName) {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    for (let i=0;i<vars.length;i++) {
        let pair = vars[i].split("=");
        if(pair[0] === paraName){return pair[1];}
    }
    return(false);
}

function getUrlParams() {
  q = {}
  location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v)
  return q
}

/**
 * 处理iOS 微信客户端6.7.4 键盘收起页面未下移bug
 */
;(/iphone|ipod|ipad/i.test(navigator.appVersion)) && document.addEventListener('blur', (e) => {
  // 这里加了个类型判断，因为a等元素也会触发blur事件
  ['input', 'textarea'].includes(e.target.localName) && document.body.scrollIntoView(false)
}, true)
